package com.sny.controller;

import com.sny.constants.ResultForAmis;
import com.sny.service.IAmisChunkFileService;
import com.sny.vo.param.AmisFinishChunkParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author: Lilinwe
 * @Description: amis文件分块上传
 * @Date: 2023/01/09 10:25:28
 * @Modified:
 */
@Slf4j
@Controller
@RequestMapping(value = {"/api"})
public class AmisChunkFileController {

    @Autowired
    private IAmisChunkFileService amisChunkFileService;

    /**
     * startChunkApi  用来做分块前的准备工作
     * @param filename 文件名
     * @param name
     * @param appId
     * @return
     */
    @ResponseBody
    @PostMapping("/startChunkApi")
    public ResultForAmis startChunkApi(String filename, String name, String appId) {
        return amisChunkFileService.startChunkApi(filename, name, appId);
    }

    /**
     * chunkApi 用来接收每个分块上传,并将文件进行合并
     * @param key startChunkApi返回的 key
     * @param uploadId startChunkApi返回的 uploadId
     * @param partNumber 分块序号，从 1 开始。
     * @param partSize 分块大小
     * @param file 文件
     * @param appId
     * @return
     */
    @ResponseBody
    @PostMapping("/chunkApi")
    public ResultForAmis chunkApi(String key, String uploadId, Long partNumber, Long partSize, @RequestParam("file") MultipartFile file, String appId) {
        return amisChunkFileService.chunkApi(key, uploadId, partNumber, partSize, file, appId);
    }

    // finishChunkApi

    /**
     * finishChunkApi 分块上传后 将合并后的文件信息保存到数据库
     * @param param
     * @param appId
     * @return
     */
    @ResponseBody
    @PostMapping("/finishChunkApi")
    public ResultForAmis finishChunkApi(@RequestBody AmisFinishChunkParam param, String appId) {
        return amisChunkFileService.finishChunkApi(param, appId);
    }

}
